/*
 * Communication test program for various type of communication.
 * @author Beke András (beke.andras.mkolc@gmail.com)
 * Thanks for Raphael Blatter (raphael@blatter.sg)
 * @author heavily using code examples from the RXTX-website (rxtx.qbang.org)
 */
package iotester;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;          // A soros portok listájához

/**
 * @author bekeband
 */
public class IOTester {

/*
   * default serial speed
   */
	public static int speed = 9600;

  /*
   * Instance of serial communication class of RXTX library {@link SerialComm}
   */
  
  private static SerialComm Comm;
  
  /**
   * @param args the command line arguments
   */
  public static void main(String[] args) {
    
    boolean DEBUG_ON;
/*
     * DEBUG ???????????????????????????
     */
    DEBUG_ON = ("ON".equals(System.getProperty("DEBUG")));

		String input;
		BufferedReader in_stream = new BufferedReader(new InputStreamReader(
				System.in));    
    
/*
* Made for Serial communication instance.
*/
    
		Comm = new SerialComm((int)0);
    if (args.length > 0)
    { 
      
/*
 * Baud rate data reading from arguments.
 */
			try {
				speed = Integer.parseInt(args[0]);
			} catch (NumberFormatException e) {
				System.out.println("a sebességnek számnak kell lennie.\n");
				System.out.printf("Alapértelmezett sebesség használata: %u baud.\n", speed);        
			}
     }

/*
 * List of usuable serial ports.
 */
        
		List<String> ports = Comm.getPortList();

/*
 * Those lines of experimental tests.
 * @TODO : 
 * -----------------------------------------------------------------
 */
    
String CName = "/dev/ttyUSB0";    
    
    if (Comm.connect(CName, speed))
    {
			System.out.printf("%s port succesfully opened\n", CName);
    }else
    {
			System.out.printf("Cannot open %s port\n", CName);
			System.exit(1);
    }
    
    try {   
    input = in_stream.readLine();
				if (input.equals("q")) {
					System.out.println("example terminated\n");
					Comm.disconnect();
					System.exit(0);   
        }
  } catch (IOException e) {
				System.out.println("there was an input error\n");
				System.exit(1);
  };
  
/*
 * End of experimental tests.
 * -----------------------------------------------------------------
 */
  
  
  }
}